package com.woniuxy.carlife.carlifeuserweb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import java.nio.charset.Charset;

/**
 * @author hxy
 * @ClassName:RedisConfig
 * @Description:redis配置类
 * @date 2021/4/7 15:11
 **/

@Configuration
public class RedisConfig {

    /**
     * 功能描述〈配置redistemplate，设置序列化机制〉
     *
     * @throws
     * @author:hxy
     * @param: RedisConnectionFactory
     * @return: RedisTemplate
     * @Date 2021/4/7 15:16
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置Redis连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(Charset.forName("UTF-8"));
        //设置key的序列化机制
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        //设置value的序列化机制
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        return redisTemplate;
    }
}
